﻿using MesBSW.BLL.Role;
using MesBSW.DAL.RoleDB;
using MesBSW.Tools;
using MesBSW.View.Dialog.Role;
using Sunny.UI;
using System;
using System.Windows.Forms;

namespace MesBSW.View.RolePages
{
    public partial class RoleInfoFrm : UIPage
    {

        private RoleInfoControler _my_ctrl = new RoleInfoControler();

        public RoleInfoFrm()
        {
            InitializeComponent();
        }


        private void InitDgvRoleInfo() 
        {
            dgvRoleInfo.Init();
            dgvRoleInfo.AddColumn("ID", "ID");
            dgvRoleInfo.AddColumn("role_code", "role_code").HeaderText = "编号";
            dgvRoleInfo.AddColumn("role_name", "role_name").HeaderText = "角色名称";
            dgvRoleInfo.AddColumn("create_user", "create_user").HeaderText ="创建人";
            dgvRoleInfo.AddColumn("create_date", "create_date").HeaderText = "创建时间";
            InitDgvRoleInfoData();
        }


        private void InitDgvUserRole()
        {
            dgvUserRole.Init();
            dgvUserRole.AddColumn("ID", "ID").HeaderText="用户ID";
            dgvUserRole.AddColumn("user_name", "user_name").HeaderText = "账号";
            dgvUserRole.AddColumn("real_name", "real_name").HeaderText = "姓名";
            dgvUserRole.AddColumn("role_name", "role_name").HeaderText = "角色名称";
            dgvUserRole.AddColumn("base_role_id", "base_role_id").Visible = false;
            dgvUserRole.AddColumn("create_user", "create_user").HeaderText = "创建人";
            dgvUserRole.AddColumn("create_date", "create_date").HeaderText = "创建时间";
            dgvUserRole.AddColumn("update_date", "update_date").HeaderText = "修改时间";
            InitDgvUserRoledata();
        }



        private void InitDgvRoleInfoData() 
        {
            dgvRoleInfo.DataSource = _my_ctrl.getRoles();
        }

        private void InitDgvUserRoledata()
        {
            dgvUserRole.DataSource = _my_ctrl.getUserRoles();
        }

        private void btnImportUser_Click(object sender, EventArgs e)
        {

            BaseRoleModel roleModel = new BaseRoleModel();
            roleModel.id = _my_ctrl.getRoleLastId()+1;
            AddRoleDia frm = new AddRoleDia(roleModel);
            frm.Render();
            frm.ShowDialog();

            if (frm.IsOK) 
            {
                InitDgvRoleInfoData();
            }
        }



        private void btnRoleAuth_Click(object sender, EventArgs e)
        {
            int selectIndex = dgvRoleInfo.SelectedIndex;
            var roleId = dgvRoleInfo.Rows[selectIndex].Cells["ID"].Value.ToString().ToInt();
            AuthRoleDia frm = new AuthRoleDia(roleId,_my_ctrl);
            frm.Render();
            frm.ShowDialog();
        }

        private void dgvRoleInfo_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var roleId = dgvRoleInfo.Rows[e.RowIndex].Cells["ID"].Value.ToString().ToInt();
            var roleModel = _my_ctrl.getRoleInfo(roleId);
            AddRoleDia frm = new AddRoleDia(roleModel);
            frm.Text = "修改ID：" + roleModel.id;
            frm.Render();
            frm.ShowDialog();
            if (frm.IsOK)
            {
                InitDgvRoleInfoData();
            }
        }

        private void btndelRole_Click(object sender, EventArgs e)
        {
            int selectIndex = dgvRoleInfo.SelectedIndex;
            var roleId = dgvRoleInfo.Rows[selectIndex].Cells["ID"].Value.ToString().ToInt();
            _my_ctrl.delRole(roleId);
            UIMessageBox.Show(SysTools.errMsg);
            InitDgvRoleInfoData();
        }

        private void dgvUserRole_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var userId = dgvUserRole.Rows[e.RowIndex].Cells["ID"].Value.ToString().ToInt();
            AddUserRoleDia frm = new AddUserRoleDia(userId,_my_ctrl);
            frm.Text =  "修改用户角色";
            frm.Render();
            frm.ShowDialog();
            if (frm.IsOK)
            {
                InitDgvUserRoledata();
            }

        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            InitDgvRoleInfoData();
            InitDgvUserRoledata();
        }

        private void uiHeaderButton1_Click(object sender, EventArgs e)
        {
            string con = "";
            if (this.InputStringDialog(ref con, true, "请输入角色名称：", false))
            {
                dgvRoleInfo.DataSource = _my_ctrl.getRoles(con);

            };
        }

        private void uiHeaderButton2_Click(object sender, EventArgs e)
        {
            string con = "";
            if (this.InputStringDialog(ref con, true, "请输入名称：", false))
            {
                dgvUserRole.DataSource = _my_ctrl.getUserRoles(0, con);
            };
        }

        private void RoleInfoFrm_Load(object sender, EventArgs e)
        {
            InitDgvRoleInfo();
            InitDgvUserRole();
        }

        /// <summary>
        /// 角色权限绑定部门
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiHeaderButton3_Click(object sender, EventArgs e)
        {
            UIMessageBox.ShowWarning("该功能贝斯旺暂不需要！");
            //int selectIndex = dgvRoleInfo.SelectedIndex;
            //int roleId = dgvRoleInfo.Rows[selectIndex].Cells["ID"].Value.ToString().ToInt();
            //string role_name = dgvRoleInfo.Rows[selectIndex].Cells["role_name"].Value.ToString();
            //RoleDepartmentDia frm = new RoleDepartmentDia(roleId,role_name);
            //frm.Render();
            //frm.ShowDialog();
        }
    }
}
